<?php

namespace app\admin\controller\annalgame;

use app\common\controller\Backend;

use think\Controller;
use think\Request;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Annalstatistics extends Backend
{
    /**
     * 跨库关联查询关联键
     */
    protected $kuamap = [];
    
      /**
     * 跨库关联查询关联键
    */
    protected $kuaid = 'uid';

    /**
     * 跨库关联读取关建键表
     */
    protected $kuatablea = 'fa_v_annal_game';

    /**
     * 跨库关联提取字段表
     */
    protected $kuatableb = 'user';

    /**
     * 跨库关联提取字段字符串
     */
    protected $fieldstr = 'uid,nickname,sex,level';
    /**
     * VAnnalGame模型对象
     */
    protected $model = null;

    public function _initialize()
    { 
        parent::_initialize();
        $this->model = model('VAnnalGame');
    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
     * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
        public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);

        //进行获取信息
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('pkey_name'))
            {   
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            
            //搜索条件
            $filter = $this->request->get("filter", '');
            $filter_arr = json_decode($filter,true);
            $map = [];
            foreach ($filter_arr as $key=>$val){
                if($key == 'starttime'){
                    $time_arr = explode(',', $filter_arr['starttime']);
                    $star = $this->getMilliDate($time_arr[0]);
                    $end = $this->getMilliDate($time_arr[1]);
                    if(empty($time_arr[0])){
                        $map['starttime'] = array('<=', $end);
                    }elseif(empty($time_arr[1])){
                        $map['starttime'] = array('>=', $star);
                    }else{
                        $map['starttime'] = array('between', [$star,$end]);
                    }
                }else{
                    $map[$key] = $val;
                }
            }
            
            $total = $this->model
                    ->where($map)
                    ->order($sort, $order)
                    ->count();

            $list = $this->model
                    ->where($map)
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();    

            $uid_arr = [];
                //加入游戏时长      
            foreach($list as $value){ 
                $uid_arr[] = $value['uid'];
                if (!empty($value['endtime']))
                {   
                   $gametime_new = $this->timeDiff($value['starttime'],$value['endtime']);
                   $value['gametime'] = $this->Sec2Time($gametime_new);
                }
            }
            $this->kuamap = $uid_arr;
            //关联查询数据       
            $result = array("total" => $total, "rows" => $this->SetNamepost($this->kuakfunc(),$list));

            return json($result);
        }
        
        return $this->view->fetch();
    }    

}
